//index.js
//获取应用实例
var util = require('../../utils/util.js');
var WxSearch = require('../wxSearch/wxSearch.js');
const app = getApp()

Page({
  data: {
    tabStatus: {
      hdindex: 1,
      bdindex: 1
    },

    curPage: [0, 0, 0, 0],
    curPageSta: [true, true, true, true],

    visited: [],

    category:[
      "公共教材", "专业教材", "专业书籍", "课外书籍"
    ],

    isSearch: false,

    limit: 200,

    currentList: []
  },
  //事件处理函数

  /**
   * 用户点击书籍item转到详情页
   */
  toDetail: function (e) {
    var bookid = e.currentTarget.dataset.book;
    wx.navigateTo({
      url: '/pages/bookDetail/bookDetail?id='+bookid
    })
  },

  /**
   * 点击发布按钮转到发布页面
   */
  postBook: function (e) {
    wx.navigateTo({
      url: '../publishbooks/publishbooks'
    })
  },

  /**
   * 点击搜索键转到搜索页面
   */
  wxSearchFn: function (e) {
    var that = this
    WxSearch.wxSearchAddHisKey(that);
    wx.navigateTo({
      url: '../search/searchRes?searchWrd='+this.data.wxSearchData.value
    })
  },

  wxSearchInput: function (e) {
    var that = this
    WxSearch.wxSearchInput(e, that);
  },

  wxSerchFocus: function (e) {
    var that = this
    WxSearch.wxSearchFocus(e, that);
  },

  wxSearchBlur: function (e) {
    var that = this
    WxSearch.wxSearchBlur(e, that);
  },

  wxSearchKeyTap: function (e) {
    var that = this
    WxSearch.wxSearchAddHisKey(that);
    WxSearch.wxSearchKeyTap(e, that);
  },

  wxSearchDeleteKey: function (e) {
    var that = this
    WxSearch.wxSearchDeleteKey(e, that);
  },

  wxSearchDeleteAll: function (e) {
    var that = this;
    WxSearch.wxSearchDeleteAll(that);
  },

  wxSearchTap: function (e) {
    var that = this
    WxSearch.wxSearchHiddenPancel(that);
  },

  changeList: function(event){
    var that = this;
    var barType = event.target.dataset.type;
    this.loadBooks(1, barType, list => {
      var curPage_ = [0, 0, 0, 0];
      curPage_[barType - 1] = curPage_[barType-1] + 1;
      that.setData({
        currentList: list,
        curPage: curPage_,
        curPageSta: [true, true, true, true],
        tabStatus: {
          hdindex: barType,
        },
      });
    })
  },

  scrollLoad: function(e){
    console.log("addmore");
    var that = this;
    this.loadBooks(this.data.tabStatus.bdindex, list=>{
      var curlist = that.data.currentList;
      for(var i = 0; i < list.length; i++)
        curlist.push(list[i]);
      that.setData({
        currentList: curlist,
      });
    });
  },

  onLoad: function () {
    var that = this;
    //初始化的时候渲染wxSearchdata
    WxSearch.init(that, 43, ['算法导论', '机器学习', '世界通史', '线性代数', '离散数学与应用', '大学物理', '围城']);
    WxSearch.initMindKeys(['深入理解Java虚拟机', 'Unix网络编程', '世界通史', '线性代数']);

    this.loadBooks(that.data.curPage[0]+1, 1, list=>{
      var curPage_ = that.data.curPage;
      curPage_[0] = curPage_[0] + 1;
      that.setData({
        currentList: list,
        curPage: curPage_,
        windowHeight: wx.getSystemInfoSync().windowHeight
      });
    })
  },

  loadBooks: function(page, category, callback){
    var that = this;
    wx.showLoading({
      title: '加载中...',
    });
    console.log(category)
    console.log(page)
    if(that.data.curPageSta[category-1])
    {
      wx.request({
        url: 'https://bookin.moha.vip/book/book_list/',
        method: "POST",
        data: {
          page: page,
          category: parseInt(category)
        },
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
          if (res.statusCode == 200) {
            var list = [];
            for (var i = 0; i < res.data.data.length; i++) {
              var item = {};
              item.book_id = res.data.data[i].book_id;
              item.imgUrl = res.data.data[i].book_pic_first;
              item.title = res.data.data[i].book_name;
              item.course = res.data.data[i].course;
              item.price = res.data.data[i].price;
              item.notes = res.data.data[i].notes;
              list.push(item);
            }
            if (callback && typeof (callback) == 'function') {
              callback(list);
            }
          }
          else {
            var cps = that.data.curPageSta;
            cps[category - 1] = false;
          }
        },
        fail: function (e) {
          console.log(e)
        },
        complete: function () {
          wx.hideLoading();
        }
      })
    }
    wx.hideLoading();
  },

  getUserInfo: function(e) {
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },

  onShow: function(){
    var that = this;
    wx.getClipboardData({
      success: function (res) {
        console.log(res);
        var data = res.data;
        var pattern = /BOOKIN\/book\/\d+\//
        var mt = data.match(pattern);
        console.log(mt);

        if (mt === null) return;
        var url = mt[0];
        var id = url.match(/\d+/);
        if(id === null) return;
        id = id[0];
        var visited = that.data.visited;
        if (visited.indexOf(id) !== -1){
          return;
        }

        visited.push(id);
        that.setData({
          visited: visited
        });

        wx.getStorage({
          key: 'cookie',
          success: function (res) {
            var cookie = res.data;
            var header = util.getCommonHeaders(cookie);
            wx.request({
              url: 'https://bookin.moha.vip/book/' + id + "/",
              header: header,
              success: function (res) {
                if(res.statusCode != 200) return;
                wx.showModal({
                  title: '跳转',
                  content: '检查到剪贴板有 ' + res.data.data["book_name"] + " 请问是否跳转？",
                  success: function (res) {
                    if(res.confirm)
                      wx.navigateTo({
                        url: '/pages/bookDetail/bookDetail?id=' + id,
                      });
                  }
                })
              },
            })
          },
          fail: () => {
            wx.hideLoading();
          }
        });
      }
    })
  },

   /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log("addmore");
    var that = this;
    var catelog = this.data.tabStatus.hdindex;
    this.loadBooks(that.data.curPage[catelog-1]+1, catelog, list => {
      var curlist = that.data.currentList;
      var curPage_ = that.data.curPage;
      curPage_[catelog - 1] = curPage_[catelog - 1]+1;
      for (var i = 0; i < list.length; i++)
        curlist.push(list[i]);
      that.setData({
        currentList: curlist,
        curPage: curPage_
      });
    });
  },

  /**
  * 页面相关事件处理函数--监听用户下拉动作
  */
  onPullDownRefresh: function () {
    console.log("refresh");
    var that = this;
    var catelog = this.data.tabStatus.hdindex;
    this.loadBooks(1, catelog, list => {
      var curPage_ = that.data.curPage;
      var curSta_ = that.data.curPageSta;
      curPage_[catelog - 1] = 1;
      curSta_[catelog - 1] = true;
      that.setData({
        currentList: list,
        curPage: curPage_,
        curPageSta: curSta_
      });
    });
    wx.stopPullDownRefresh();
  }
})
